<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto Serif SC:300,300italic,400,400italic,700,700italic|Roboto Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib/pace/pace-theme-bounce.min.css">
  <script src="/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"zhiruozzy.cn","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":"truw","sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="布局原理flex是flexible Box 的缩写，是弹性布局的意思。">
<meta property="og:type" content="article">
<meta property="og:title" content="flex布局">
<meta property="og:url" content="http://zhiruozzy.cn/2022/03/23/flex%E5%B8%83%E5%B1%80/index.html">
<meta property="og:site_name" content="芷若">
<meta property="og:description" content="布局原理flex是flexible Box 的缩写，是弹性布局的意思。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213110551.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213728101.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213753046.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213813801.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213905360.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323214852302.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323215031321.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323215241090.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323215351121.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323215414603.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323220839174.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323221003410.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323221028283.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323222102413.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323222144296.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323215951913.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323220019157.png">
<meta property="og:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323220132789.png">
<meta property="article:published_time" content="2022-03-23T13:11:08.000Z">
<meta property="article:modified_time" content="2022-04-06T01:05:53.937Z">
<meta property="article:author" content="芷若">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://zhiruozzy.cn/2022/03/23/img/flex布局/image-20220323213110551.png">

<link rel="canonical" href="http://zhiruozzy.cn/2022/03/23/flex%E5%B8%83%E5%B1%80/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>flex布局 | 芷若</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<!-- hexo injector head_end start -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hexo-math@4.0.0/dist/style.css">
<!-- hexo injector head_end end --><link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css">
<link rel="stylesheet" href="/css/prism-line-numbers.css" type="text/css"></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">芷若</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-主页">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>主页</a>

  </li>
        <li class="menu-item menu-item-关于">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-标签">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-目录">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>目录</a>

  </li>
        <li class="menu-item menu-item-归档">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container"></div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="algolia-results">
  <div id="algolia-stats"></div>
  <div id="algolia-hits"></div>
  <div id="algolia-pagination" class="algolia-pagination"></div>
</div>

      
    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/zhiruozzy" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://zhiruozzy.cn/2022/03/23/flex%E5%B8%83%E5%B1%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/touxiang.jpg">
      <meta itemprop="name" content="芷若">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="芷若">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          flex布局
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-03-23 21:11:08" itemprop="dateCreated datePublished" datetime="2022-03-23T21:11:08+08:00">2022-03-23</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-04-06 09:05:53" itemprop="dateModified" datetime="2022-04-06T09:05:53+08:00">2022-04-06</time>
              </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.1k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="布局原理"><a href="#布局原理" class="headerlink" title="布局原理"></a>布局原理</h2><p>flex是flexible Box 的缩写，是弹性布局的意思。</p>
<span id="more"></span>

<ul>
<li>块级元素和行内元素可以指定为flex布局</li>
<li>将父元素设置为flex布局后，子元素的浮动，清除浮动，vertical-align的属性都会失效</li>
<li>父元素称为“容器”，子元素称为“项目”，子元素可以横向排列也可以纵向排列</li>
</ul>
<p>通过给父元素增加flex属性来控制子元素的位置和排列方式</p>
<p><strong>注意</strong>：</p>
<p>以下所有属性的演示都是基于以下HTML</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">div</span>&#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">display</span>: flex; </span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>:<span class="number">800px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">300px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background-color</span>: <span class="number">#74c0fc</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css">    </span></span><br><span class="line"><span class="language-css">    <span class="selector-tag">div</span> <span class="selector-tag">span</span>&#123;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">width</span>: <span class="number">150px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">height</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">        <span class="attribute">background-color</span>: <span class="number">#1c7ed6</span>;</span></span><br><span class="line"><span class="language-css">    &#125;</span></span><br><span class="line"><span class="language-css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span>&gt;</span>1<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span>&gt;</span>2<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span>&gt;</span>3<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>初始界面：</strong></p>
<img src="../img/flex布局/image-20220323213110551.png" alt="image-20220323213110551" style="zoom: 25%;" />

<ul>
<li><p>设置主轴方向：flex-direction</p>
<table>
<thead>
<tr>
<th>属性值</th>
<th>主轴方向</th>
</tr>
</thead>
<tbody><tr>
<td>row</td>
<td>系统<strong>默认值</strong>，从左到右</td>
</tr>
<tr>
<td>row-reverse</td>
<td>从右到左</td>
</tr>
<tr>
<td>column</td>
<td>从上到下</td>
</tr>
<tr>
<td>column-reverse</td>
<td>从下到上</td>
</tr>
</tbody></table>
</li>
<li><p>设置主轴上子元素的排列方式: justify-content </p>
<table>
<thead>
<tr>
<th>属性值</th>
<th>子元素的排列方式</th>
</tr>
</thead>
<tbody><tr>
<td>flex-start</td>
<td>系统<strong>默认值</strong>，从头开始，如果主轴为x轴，则从左到右</td>
</tr>
<tr>
<td>flex-end</td>
<td>从尾部开始排列</td>
</tr>
<tr>
<td>center</td>
<td>从主轴居中对齐（若主轴是x轴则元素水平居中）</td>
</tr>
<tr>
<td>space-around</td>
<td>平分剩余空间</td>
</tr>
<tr>
<td>space-between</td>
<td>两边贴边，再平分剩余空间</td>
</tr>
</tbody></table>
</li>
</ul>
<h2 id="父项属性"><a href="#父项属性" class="headerlink" title="父项属性"></a>父项属性</h2><h3 id="设置主轴的方向：flex-direction"><a href="#设置主轴的方向：flex-direction" class="headerlink" title="设置主轴的方向：flex-direction"></a>设置主轴的方向：flex-direction</h3><p>子元素跟着主轴来排列</p>
<p><code>flex-direction:row; </code> 默认，子元素从左到右排列</p>
<img src="../img/flex布局/image-20220323213728101.png" alt="image-20220323213728101" style="zoom: 25%;" />

<p><code>flex-direction:row-reverse;</code>   子元素从右到左排列</p>
<img src="../img/flex布局/image-20220323213753046.png" alt="image-20220323213753046" style="zoom:25%;" />

<p><code>flex-direction:column; </code> 子元素从上到下排列</p>
<img src="../img/flex布局/image-20220323213813801.png" alt="image-20220323213813801" style="zoom:25%;" />

<p><code>flex-direction:column-reverse；</code>子元素从下向上排列</p>
<img src="../img/flex布局/image-20220323213905360.png" alt="image-20220323213905360" style="zoom:25%;" />

<h3 id="设置主轴上子元素的排列方式-justify-content"><a href="#设置主轴上子元素的排列方式-justify-content" class="headerlink" title="设置主轴上子元素的排列方式: justify-content"></a>设置主轴上子元素的排列方式: justify-content</h3><p><code>justify-content ：flex-start</code></p>
<img src="../img/flex布局/image-20220323214852302.png" alt="image-20220323214852302" style="zoom:25%;" />

<p><code>justify-content ：flex-end</code></p>
<p>这里注意区分与<code>flex-direction:row-reverse;</code>的区别，这个的元素依旧是从左向右的方向的，只不过靠右紧贴</p>
<img src="../img/flex布局/image-20220323215031321.png" alt="image-20220323215031321" style="zoom:25%;" />

<p><code>justify-content :center</code></p>
<img src="../img/flex布局/image-20220323215241090.png" alt="image-20220323215241090" style="zoom:25%;" />

<p><code>justify-content ：space-around</code></p>
<img src="../img/flex布局/image-20220323215351121.png" alt="image-20220323215351121" style="zoom:25%;" />

<p><code>justify-content ：space-between</code></p>
<img src="../img/flex布局/image-20220323215414603.png" alt="image-20220323215414603" style="zoom:25%;" />

<h3 id="设置侧轴上的子元素的排列方式：align-items（单行）"><a href="#设置侧轴上的子元素的排列方式：align-items（单行）" class="headerlink" title="设置侧轴上的子元素的排列方式：align-items（单行）"></a>设置侧轴上的子元素的排列方式：align-items（单行）</h3><p><strong>注意</strong>，该属性只能在子元素只有单行的时候才可以</p>
<p>控制子元素在侧轴（默认是y轴）上的排列方式</p>
<p><code>align-items:flex-start</code>:默认值，从上到下排列</p>
<img src="../img/flex布局/image-20220323220839174.png" alt="image-20220323220839174" style="zoom:25%;" />

<p><code>align-items:center</code>:居中</p>
<img src="../img/flex布局/image-20220323221003410.png" alt="image-20220323221003410" style="zoom:25%;" />

<p><code>align-items:flex-end</code>:从下到上排列</p>
<img src="../img/flex布局/image-20220323221028283.png" alt="image-20220323221028283" style="zoom:25%;" />

<h3 id="设置侧轴上的子元素的排列方式：align-content（多行）"><a href="#设置侧轴上的子元素的排列方式：align-content（多行）" class="headerlink" title="设置侧轴上的子元素的排列方式：align-content（多行）"></a>设置侧轴上的子元素的排列方式：align-content（多行）</h3><p><strong>注意</strong>：该属性只有在子元素换行的时候才可以使用，即使用该属性首先要<code>flex-wrap:wrap</code>.</p>
<p><code>align-content:flex-start;</code>默认值，从侧轴的头部开始排列</p>
<img src="../img/flex布局/image-20220323222102413.png" alt="image-20220323222102413" style="zoom:33%;" />

<p><code>align-content:center;</code>默认值，从侧轴的中间显示</p>
<img src="../img/flex布局/image-20220323222144296.png" alt="image-20220323222144296" style="zoom: 50%;" />

<p><code>align-content:space-around;</code>子元素在侧轴评分剩余元素</p>
<p><code>align-content:space-between;</code>子元素在侧轴先分布在两头，在平分剩余空间</p>
<p><code>align-content:stretch</code>:子元素不设置高度，子严肃的高度评分父元素高度</p>
<h3 id="设置子元素是否换行-：flex-wrap"><a href="#设置子元素是否换行-：flex-wrap" class="headerlink" title="设置子元素是否换行 ：flex-wrap"></a>设置子元素是否换行 ：flex-wrap</h3><p>在演示这个属性，为了清楚，我给子元素增加了左外边距</p>
<img src="../img/flex布局/image-20220323215951913.png" alt="image-20220323215951913" style="zoom: 50%;" />

<p>我又增加了3个子元素</p>
<img src="../img/flex布局/image-20220323220019157.png" alt="image-20220323220019157" style="zoom: 50%;" />

<p><code>flex-wrap:nowrap</code>(默认不换行，如果父元素一行内子元素放不下，会自动缩小子元素的宽度)</p>
<p>可以发现，增加了子元素之后一行明显放不下了，所以系统默认将子元素的宽度缩小了。</p>
<p><code>flex-wrap:wrap</code></p>
<img src="../img/flex布局/image-20220323220132789.png" alt="image-20220323220132789" style="zoom: 50%;" />

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2022/03/18/%E9%93%BE%E8%A1%A8%E7%AC%AC%E4%B8%80%E6%AC%A1%E4%BD%9C%E4%B8%9A/" rel="prev" title="链表第一次作业">
      <i class="fa fa-chevron-left"></i> 链表第一次作业
    </a></div>
      <div class="post-nav-item">
    <a href="/2022/03/27/HTTP/" rel="next" title="HTTP自学笔记">
      HTTP自学笔记 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B8%83%E5%B1%80%E5%8E%9F%E7%90%86"><span class="nav-number">1.</span> <span class="nav-text">布局原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%88%B6%E9%A1%B9%E5%B1%9E%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">父项属性</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E4%B8%BB%E8%BD%B4%E7%9A%84%E6%96%B9%E5%90%91%EF%BC%9Aflex-direction"><span class="nav-number">2.1.</span> <span class="nav-text">设置主轴的方向：flex-direction</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E4%B8%BB%E8%BD%B4%E4%B8%8A%E5%AD%90%E5%85%83%E7%B4%A0%E7%9A%84%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F-justify-content"><span class="nav-number">2.2.</span> <span class="nav-text">设置主轴上子元素的排列方式: justify-content</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E4%BE%A7%E8%BD%B4%E4%B8%8A%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E7%9A%84%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F%EF%BC%9Aalign-items%EF%BC%88%E5%8D%95%E8%A1%8C%EF%BC%89"><span class="nav-number">2.3.</span> <span class="nav-text">设置侧轴上的子元素的排列方式：align-items（单行）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E4%BE%A7%E8%BD%B4%E4%B8%8A%E7%9A%84%E5%AD%90%E5%85%83%E7%B4%A0%E7%9A%84%E6%8E%92%E5%88%97%E6%96%B9%E5%BC%8F%EF%BC%9Aalign-content%EF%BC%88%E5%A4%9A%E8%A1%8C%EF%BC%89"><span class="nav-number">2.4.</span> <span class="nav-text">设置侧轴上的子元素的排列方式：align-content（多行）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AE%E5%AD%90%E5%85%83%E7%B4%A0%E6%98%AF%E5%90%A6%E6%8D%A2%E8%A1%8C-%EF%BC%9Aflex-wrap"><span class="nav-number">2.5.</span> <span class="nav-text">设置子元素是否换行 ：flex-wrap</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="芷若"
      src="/images/touxiang.jpg">
  <p class="site-author-name" itemprop="name">芷若</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">19</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
        <span class="site-state-item-count">5</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/zhiruo" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhiruo" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">陕ICP备2022001874号 </a>
      <img src="http://www.beian.gov.cn/portal/download" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="芷若"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">芷若</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">105k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:35</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="//cdn.jsdelivr.net/npm/algoliasearch@4/dist/algoliasearch-lite.umd.js"></script>
<script src="//cdn.jsdelivr.net/npm/instantsearch.js@4/dist/instantsearch.production.min.js"></script>
<script src="/js/algolia-search.js"></script>














  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/assets/koharu.model.json"},"display":{"position":"right","width":145,"height":315},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
